package cn.itcast.listener;


import cn.itcast.utils.MailUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.util.Map;

/**
 * 消息监听器
 *    实现MessageListener接口
 */
public class MailListener implements MessageListener {

    //当队列中有最新消息时，自动的执行onMessage方法
    //message.getBody : 消息数据
    public void onMessage(Message message) {
        //1、获取消息数据
        byte[] body = message.getBody();
        Map<String,String> map = JSON.parseObject(body, Map.class);//将byte数组转化为map对象
        //2、调用工具类发送邮件
        String to = map.get("to");
        String subject = map.get("subject");
        String content = map.get("content");
        System.out.println(to);
        System.out.println(subject);
        System.out.println(content);
        try {
            MailUtil.sendMsg(to, subject,content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
